package com.five.years.common.utils;


import com.five.years.common.Constants;
import com.five.years.common.SystemCode;
import com.five.years.common.exception.BusinessException;
import com.five.years.common.vo.LoginUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author haiqing.zhang
 * @Date 2020-05-14
 * @Description 与token相关的功能
 */
@Slf4j
public class TokenUtil {
    /**
     * 查询用户登陆信息，当用户登陆信息存在时，返回，不存在时，返回null
     *
     * @return
     */
    public static LoginUserVO haveLoginUserInfo() {
        if (RequestContextHolder.getRequestAttributes() == null) {
            return null;
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String key = request.getHeader(Constants.HTTP_HEADER_ACCESS_TOKEN);
        if (StringUtils.isBlank(key)) {
            return null;
        }
        String loginUserString = RedisUtils.REDIS_TOKEN.get(key);
        if (StringUtils.isNotBlank(loginUserString)) {
            return JsonUtil.jsonToPojo(loginUserString, LoginUserVO.class);
        }
        if (Constants.TRUE.equals(RedisUtils.REDIS_TOKEN.hGet(Constants.RedisKey.LOGIN_EXTRUDE, key))) {
            RedisUtils.REDIS_TOKEN.hDel(Constants.RedisKey.LOGIN_EXTRUDE, key);
            return null;
        }
        return null;
    }
    public static LoginUserVO getLoginUserInfo() {
        if (RequestContextHolder.getRequestAttributes() == null) {
            throw new BusinessException(SystemCode.SIGNIN_TIMEOUT);
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String key = request.getHeader(Constants.HTTP_HEADER_ACCESS_TOKEN);
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(SystemCode.TOKEN_EXPIRE);
        }
        String loginUserString = RedisUtils.REDIS_TOKEN.get(key);
        if (StringUtils.isNotBlank(loginUserString)) {
            return JsonUtil.jsonToPojo(loginUserString, LoginUserVO.class);
        }
        if (Constants.TRUE.equals(RedisUtils.REDIS_TOKEN.hGet(Constants.RedisKey.LOGIN_EXTRUDE, key))) {
            RedisUtils.REDIS_TOKEN.hDel(Constants.RedisKey.LOGIN_EXTRUDE, key);
            throw new BusinessException(SystemCode.TOKEN_INVALID);
        }
        throw new BusinessException(SystemCode.TOKEN_EXPIRE);
    }

    public static LoginUserVO getLoginUserInfo(HttpServletRequest request) {
        String key = request.getHeader(Constants.HTTP_HEADER_ACCESS_TOKEN);
        if (StringUtils.isBlank(key)) {
            throw new BusinessException(SystemCode.TOKEN_EXPIRE);
        }
        String loginUserString = RedisUtils.REDIS_TOKEN.get(key);
        if (StringUtils.isNotBlank(loginUserString)) {
            return JsonUtil.jsonToPojo(loginUserString, LoginUserVO.class);
        }
        if (Constants.TRUE.equals(RedisUtils.REDIS_TOKEN.hGet(Constants.RedisKey.LOGIN_EXTRUDE, key))) {
            RedisUtils.REDIS_TOKEN.hDel(Constants.RedisKey.LOGIN_EXTRUDE, key);
            throw new BusinessException(SystemCode.TOKEN_INVALID);
        }
        throw new BusinessException(SystemCode.TOKEN_EXPIRE);
    }


    public static String createToken(LoginUserVO loginUser, Integer expireTime) {
        String token = IdGenUtils.uuid();
        RedisUtils.REDIS_TOKEN.set(token, JsonUtil.objectToJson(loginUser), expireTime);
        return token;
    }

}
